package org.example.demo3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo3.enity.ProductDTO;
import org.example.demo3.enity.product;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.example.demo3.enums.CreateTimePeriodEnum;
import org.example.demo3.enums.ProductConditionEnum;
import org.example.demo3.enums.ProductTypeEnum;
import org.example.demo3.mapper.ProductMapper;
import org.example.demo3.service.ProductService;
import org.example.demo3.untils.BeanCopyUtils;
import org.example.demo3.vo.productVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author xusen
 * @since 2025-03-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    public QueryWrapper<product> queryWrapper(productVO productVO) {
        QueryWrapper<product> queryWrapper = new QueryWrapper<>();

        // 精确匹配字段
        if (productVO.getId() != null) {
            queryWrapper.eq("id", productVO.getId());
        }
        if (productVO.getTitle() != null) {
            queryWrapper.eq("title", productVO.getTitle());
        }
        if (productVO.getUserId() != null) {
            queryWrapper.eq("user_id", productVO.getUserId());
        }
        if (productVO.getName() != null) {
            queryWrapper.eq("name", productVO.getName());
        }
        if (productVO.getType() != null) {
            queryWrapper.eq("type", productVO.getType());
        }

        // 模糊查询字段
        if (productVO.getDescription() != null) {
            queryWrapper.like("description", productVO.getDescription());
        }

        // 数值范围查询
        if (productVO.getPrice() != null) {
            queryWrapper.eq("price", productVO.getPrice());
        }
        if (productVO.getPriceMinPrice() != null && productVO.getPriceMaxPrice() != null) {
            queryWrapper.between("price", productVO.getPriceMinPrice(), productVO.getPriceMaxPrice());
        }

        // 枚举类型处理
        if (productVO.getCondition() != null) {
            queryWrapper.eq("condition", productVO.getCondition().name());
        }
        if (productVO.getCreateTime() != null) {
            queryWrapper.eq("create_time", productVO.getCreateTime().name());
        }

        // 排序
        queryWrapper.orderByDesc("create_time");

        return queryWrapper;
    }

    /**
     * 发布商品
     * @param productDTO
     * @return
     */
    @Override
    public boolean saveProducts(ProductDTO productDTO) {
        product product = new product();
        //标题 描述  发布者
        BeanUtils.copyProperties(productDTO,product);
        //商品价格
        product.setPrice(BigDecimal.valueOf(productDTO.getPrice()));
        //图片
        product.setImages(new Gson().toJson(productDTO.getImages()));
        //分类名称
        product.setName(productDTO.getType().getCategoryName());
        //分类类型
        product.setType(productDTO.getType().getCode());
        //商品成色
        product.setCondition(productDTO.getCondition().getValue());

        return this.save(product);
    }
    public Page<productVO> findPage(productVO productVO, int pageNum, int pageSize) {
        Page<product> InsurancePage = new Page<>(pageNum, pageSize);
        QueryWrapper<product> queryWrapper = queryWrapper(productVO);
        Page<product> page = page(InsurancePage, queryWrapper);
        Page<productVO> productVOVOPage = BeanCopyUtils.toPage(page, productVO.class);
        return productVOVOPage;
    }

    /**
     * 获取商品详情
     *
     * @param id
     * @return
     */
    @Override
    public productVO getProducts(Integer id) {
        productVO productVo = new productVO();

        product product = this.getById(id);
        BeanUtils.copyProperties(product,productVo);
//        把json对象转化成数组列表存入product中
        String images = product.getImages();
        Gson gson = new Gson();
        List<String> imageList =gson.fromJson(images, new TypeToken<List<String>>() {}.getType());
        productVo.setImages(imageList);
        //数据库的时间
        String createTime = product.getCreateTime();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(createTime, formatter);
        //计算数据库时间和以前时间的差值
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(dateTime, now);
//        获得差值多少小时
        long days = duration.toDays();
        //一天内
        if(days<=1){
            productVo.setCreateTime(CreateTimePeriodEnum.WITHIN_1_DAY);
        }else {
            //3天内
            if (days <= 3) {
                productVo.setCreateTime(CreateTimePeriodEnum.WITHIN_3_DAYS);
            }
            //一周内
            else if (days <= 7) {
                productVo.setCreateTime(CreateTimePeriodEnum.WITHIN_1_WEEK);
            } else {
                long months = days / 30;
                //一月内
                if (months <= 1) {
                    productVo.setCreateTime(CreateTimePeriodEnum.WITHIN_1_MONTH);
                }
                //三月内
                else if (months <= 3) {
                    productVo.setCreateTime(CreateTimePeriodEnum.WITHIN_3_MONTHS);
                }
                //再久以前
                else {
                    productVo.setCreateTime(CreateTimePeriodEnum.LONG_AGO);
                }
            }
        }
        //商品成色
        String condition = product.getCondition();
//        productVo.setCondition(ProductConditionEnum.valueOf(ProductConditionEnum.class,condition));
        productVo.setCondition(ProductConditionEnum.fromValue(condition));
        //分类类型
        Integer type = product.getType();
        productVo.setName(ProductTypeEnum.fromCode(type));
        return productVo;

    }




}
